import 'package:dio/dio.dart';
import 'package:fyi/conf/build_config.dart';
import 'package:fyi/net/http_header.dart';
import 'package:fyi/utils/common_util.dart';
import 'result.dart';

class HttpClient {

  static HttpClient _instance;

  static HttpClient get instance => _getInstance();

  factory HttpClient() => _getInstance();

  HttpClient._internal(){
    _addIntercept(_dio);
  }

  static HttpClient _getInstance() {
    if(_instance == null){
      _instance = new HttpClient._internal();
    }
    return _instance;
  }

  var _dio = new Dio(
      new BaseOptions(
        baseUrl: isDebug ? "http://192.168.43.237:8080/" : "http://192.168.43.237:8080/",
        connectTimeout: 5000,
        receiveTimeout: 3000,
      )
  );

  Dio get dio => _dio;

  _addIntercept(Dio dio){
    _addLogInterceptor(dio);
    _addHeaderInterceptor(dio);
  }

  void _addLogInterceptor(Dio dio) {
    dio.interceptors.add(LogInterceptor(responseBody: true));
  }

  void _addHeaderInterceptor(Dio dio) {
    dio.interceptors.add(InterceptorsWrapper(
        onRequest:(RequestOptions options){
          var header = HttpHeader.getHeader();
          options.headers.addAll(header);
          return options;
        },
        onResponse:(Response response) {
          return response;
        },
        onError: (DioError e) {
          return e;
        }
    ));
  }


  static get(String path,{Map<String, dynamic> queryParameters}) async {
    return instance._get(path,queryParameters: queryParameters);
  }
  
  static post(String path, {data, Map<String, dynamic> queryParameters}){
    return instance._post(path,data: data,queryParameters: queryParameters);
  }

  static request(String path, {
    data,
    Map<String, dynamic> queryParameters,
    CancelToken cancelToken,
    Options options,
    ProgressCallback onSendProgress,
    ProgressCallback onReceiveProgress,
  }) async {
    return instance._request(path,
      data: data,
      queryParameters: queryParameters,
      cancelToken: cancelToken,
      options: options,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,);
  }

  _get(String path,{Map<String, dynamic> queryParameters}) async {
    return _request(path,queryParameters: queryParameters,options: _checkOptions("GET", null));
  }

  _post(String path, {data, Map<String, dynamic> queryParameters}) async {
    return _request(path,data: data,queryParameters: queryParameters,options: _checkOptions("POST", null));
  }

   _request(String path, {
    data,
    Map<String, dynamic> queryParameters,
    CancelToken cancelToken,
    Options options,
    ProgressCallback onSendProgress,
    ProgressCallback onReceiveProgress,
  }) async {
    try {
      Response response = await dio.request(path,
        data: data,
        queryParameters: queryParameters,
        cancelToken: cancelToken,
        options: options,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress,);
      if (response.statusCode == 200 || response.statusCode == 201) {
        var responseJson = response.data;
        Result result = Result.fromJson(responseJson);
        if(result.isSuccess()){
          return result;
        }else{
          //do something
          _doSomething(result);
          return result;
        }
      }else{
        //do something
        var result = Result(success: false,errorDescription: "网络请求失败");
        _doSomething(result);
        return result;
      }
    }on DioError catch (e){
      print("e : ${e}");
      //do something
      var result = Result(success: false,errorDescription: e.message);
      _doSomething(result);
      return result;
    }
  }

  _doSomething(Result result){
    print('--------网络请求错误--------');
    CommonUtil.toast(result.errorDescription);
  }

  Options _checkOptions(method, options) {
    if (options == null) {
      options = new Options();
    }
    options.method = method;
    return options;
  }

}